<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class AppStartpicModel extends BaseModel {

    public function tableName() {
        return '{{app_startpic}}'; //数据库表名称
    }

    public function rules() {
        return array(
            array('aid', 'required', 'message' => '附件aid不能为空！'),
            array('filepath', 'required', 'message' => '附件路径不能为空！'),
            array('platform,uploadtime,uploadip,picname,url,title', 'safe'),
        );
    }

    //静态模型，可直接调用模型
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function attributeLabels() {
        return array(
            'pid' => 'ID',
            'aid' => '文件ID',
            'platform' => '手机平台',
            'filepath' => '文件路径',
            'uploadtime' => '保存时间',
            'uploadip' => '保存IP',
            'picname' => '图片名称',
            'url' => '活动链接',
            'title' => '活动标题',
        );
    }

    public static function getPlatformHtmlList() {
        return array(
            0 => 'Andriod 启动图',
            1 => 'ios 启动图',
            2 => 'Andriod广告图',
            3 => 'ios广告图'
        );
    }

    //数据保存前回调处理
    protected function beforeSave() {
        if ($this->isNewRecord) {
            //改变数据
            $this->uploadtime = time();
            $this->uploadip = GuestDataModel::model()->getForwardedForIp();
        }
        return true;
    }

    /**
     * 删除图片
     * @param type $aid
     * @return boolean
     */
    public function deletePic($aid) {
        $attachmentmodel = new AttachmentModel();
        if (empty($aid)) {
            return false;
        }
        if (is_array($aid)) {
            $data = $this->findAll($this->where(array('aid' => array('IN', $aid))));
        } else {
            $data = $this->findAll($this->where(array('aid' => $aid)));
        }
        if (empty($data)) {
            return false;
        } foreach ($data as $t) {
            $t->delete();
            $attachmentmodel->deleteFileByAid($aid);
        }
        return true;
    }

    /**
     * 上传成功或获取aid 和文件路径
     * @return boolean
     */
    public function getPicInfo() {
        if (empty($_FILES)) {
            return false;
        }
        if (UploadFile::getInstance()->upload()) {
            $fileInfo = UploadFile::getInstance()->getUploadFileInfo();
            $picInfo = array();
            $platform = Yii::app()->request->getParam('platform');
            foreach ($fileInfo as $key => $t) {
                $picInfo[$key]['aid'] = $t['aid'];
                $picInfo[$key]['picname'] = $t['name'];
                $picInfo[$key]['filepath'] = ConfigModel::model()->getConfig('sitefileurl') . $t['savepath'] . $t['savename'];
                $picInfo[$key]['platform'] = $platform[$key];
            }
            if (!empty($picInfo)) {
                return $picInfo;
            } else {
                return FALSE;
            }
        } else {
            UploadFile::getInstance()->getError() ? UploadFile::getInstance()->getError() : '更新失败,请重新上传！';
        }
    }

}
